/**
 *
 * Created by Song,YeWen, 9/15/2011 11:50:27 AM
 *
 */

function validateVideoUrl(ori_url) 
{
    const URL_GET_XML = 'http://www.zappinternet.com/get_playlist.php?idvideo=%1';
    var vidId = copyBetween(ori_url, 'video/', '/');
    if (!vidId) {
	     // vid not found
	     return 'NotFoundVideo';
	}
	var html = download_page(ori_url);
    var playlist = strFormat(URL_GET_XML, vidId);
	var xml_str = download_page(playlist);
	//the last arg is xml attribute
	var flv_string = parse_xml_attribute(xml_str,'channel', 'item', 'enclosure', 'url');
    var sb = [];
      
    sb.push(flv_string);
    if(sb.length){
       //matches = html.match(/<title>(.*?)<\/title>/);
       matches = html.match(/<title>([\s\S]*?)<\/title>/);       
       if(matches){
           // <span id="duracion">01:04</span>
           var time_str = copyBetween(html, '<span id="duracion">','</span>');
           var duration = 0;
           var hour;
           var minute;
           var second;
           if (time_str.length == 5) {
               minute = time_str.substring(0,2);
               second = time_str.substring(3,5);
               duration = parseInt(minute) * 60 + parseInt(second);
           } else if(time_str.length == 8) {
               hour   = time_str.substring(0,2);
               minute = time_str.substring(3,5);
               second = time_str.substring(6,8);
               duration = parseInt(hour) * 3600 + parseInt(minute) * 60 + parseInt(second);
           }
    	   return 'success_title:' + encodeURIComponent(matches[1])
    	          + '\n video_urls:' + encodeURIComponent(sb)
    	          + '\n seconds:' + Math.round(parseInt(duration));
       }
    }
	return 'error:empty_path';
}


/* http://www.zappinternet.com/video/GoXfBegXuw/Kung-Fu-callejero-editado */
function js_get_media_url(ori_url) 
{
    const URL_GET_XML = 'http://www.zappinternet.com/get_playlist.php?idvideo=%1';
    var vidId = copyBetween(ori_url, 'video/', '/');
    var playlist = strFormat(URL_GET_XML, vidId);
	var xml_str = download_page(playlist);
	//the last arg is xml attribute
	var flv_string = parse_xml_attribute(xml_str,'channel', 'item', 'enclosure', 'url');

	return flv_string;
}
